import java.awt.*;

public class TextSlideL extends Panel {
	TextField textField;
	Scrollbar slider;
	LightsChange owner;
	double theVal;

	int scrollmin = 0, scrollmax = 1000;
	double minVal = 0.0, maxVal = 5.0;

	TextSlideL(LightsChange controller, String myLabel, double min,
double max) {
		owner = controller;
		minVal = min; maxVal = max;

		GridBagConstraints gbc = new GridBagConstraints();
		GridBagLayout gbl = new GridBagLayout();		
		setLayout(gbl);
		gbc.anchor = GridBagConstraints.WEST;
		gbc.gridx = 0;
		gbc.gridy = 0;
		Label title = new Label(myLabel);
		add(title);
		gbl.setConstraints(title,gbc);

		gbc.anchor = GridBagConstraints.EAST;
		gbc.gridx = 1;
		textField = new TextField("0.0",10);
		add(textField);
		gbl.setConstraints(textField,gbc);

		gbc.anchor = GridBagConstraints.WEST;
		gbc.gridx = 2;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.weightx = 2;
		slider = new Scrollbar(Scrollbar.HORIZONTAL,1,100,scrollmin,scrollmax);
		add(slider);
		gbl.setConstraints(slider,gbc);
		setValue(0.0);

		validate();
	}

	public boolean action(Event e, Object arg) {
		if (e.target == textField) {
			double f = getTextValue();
			if (f>=minVal) {
				theVal = f;
				setSliderValue(f);
				owner.changeParam(this);
			}
		}
		return true;
	}

	public boolean handleEvent(Event e) {
		if (e.target == slider) {
			double sv = getSliderValue();
			theVal = sv;
			textField.setText(String.valueOf(sv));
			owner.changeParam(this);
		}
        	return super.handleEvent(e);
	}

	// Set the slider and text fields to the same floating point number.
	public void setValue(double f) {
		if (f<minVal) {
			f = minVal;
		} else if (f>maxVal) {
			f = maxVal;
		}
		theVal =  f;
		textField.setText(String.valueOf(f));
		setSliderValue(f);
	}

	public double getValue() {
		return theVal;
	}

	// Calculate what slider position corresponds to a number and set theposition.
	private void setSliderValue(double f) {
		slider.setValue((int)(scrollmin+
			(f-minVal)/(maxVal-minVal)*(scrollmax-scrollmin)));
	}

	// A local function to find the number typed in the textbox.
	private double getTextValue() {
		double f;
		// Catch possible non-numbers.
		try {
			f = Double.valueOf(textField.getText()).doubleValue();
		} catch (java.lang.NumberFormatException e) {
			f = minVal-1.0;
		}
		// Don't let them kill the animation with crazy numbers.
		if (f>maxVal) {
			f = maxVal;
			textField.setText(String.valueOf(f));
		} else if (f<minVal) {
			f = minVal;
			textField.setText(String.valueOf(f));
		}
		return(f);
	}

	// Read the velocity from the integer slider position.
	private double getSliderValue() {
		double velocity;
		int val = slider.getValue()-scrollmin;
		velocity = minVal+((maxVal-minVal)*
			val)/(scrollmax-scrollmin);
		return velocity;
	}
}
